package com.library.controller.admin;

import com.library.pojo.Category;
import com.library.pojo.Result;
import com.library.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategory")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类的名字
     * @return
     */

    @GetMapping
    public Result getCategoryName(String name){
        List<Category> result = categoryService.getCategory(name);
        return Result.success(result);
    }
    /**
     * 根据id查询分类
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Category result = categoryService.getById(id);
        return Result.success(result);
    }
    /**
     * 新增分类
     */
    @PostMapping
    public Result save(@RequestBody Category category){
        categoryService.add(category);
        return Result.success();
    }
    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        categoryService.delete(id);
        return Result.success();
    }
}
